<?php
/*
 * 体现
 */
namespace Mobile\Controller;
use Mobile\Model\guestbookModel;
use Mobile\Model\tmpmsgModel;

class GuestbookController extends BaseController {
    
    public $guestbookModel;
    
    public function __construct() {
        parent::__construct();
        $this->guestbookModel = new guestbookModel();
    }
    
    
    /*
     * 定时任务
     */
    public function cronGuishudi(){
        $res = $this->_cronGuishudi();
        if(!$res){
            return_json(1,"EMPTY_DATA");
        }else{
            return_json(0,"",$res);
        }
    }
    
    /*
     * 添加留言
     */
    public function ajaxadd(){
        
        if(!IS_AJAX){
            return_json(1,"提交方式有误");
        }
        
        $allow = array(
            "realname","mobile","content",
            "article_id","article_title","network","mobile_brand"
        );
        $must = array("realname","mobile");
        
        $data = param_filter($_POST, $allow,$must,true,true);
        if(empty($data)){
            return_json(1,"请正确填写姓名和手机号码");
        }
        
        //判断有没有频繁提交
        $time1 = date("Y-m-d H:i:s",time()-5*60);
        $time2 = date("Y-m-d H:i:s");
        $where_pf = array(
            "mobile" => $data['mobile'],
         //   "uid" => $this->uid,
            "create_time" => array(
                "between",array($time1,$time2)
            )
        );
        $res = $this->guestbookModel->getGuestbook($where_pf);
        if($res){
            return_json(1,"你刚才已经留言了,不必重复提交。如有需要,请稍后再重试");
        }
        
        $data['create_time'] = date("Y-m-d H:i:s");
        
        $data['sex'] = $this->memberinfo['sex'];//性别
        $data['openid'] = $this->memberinfo['openid'];
        $data['uid'] = $this->uid;
        $data['nickname'] = $this->memberinfo['nickname'];//性别
        $data['headimgurl'] = $this->memberinfo['headimgurl'];
        
        $data['mobile_os'] = get_device_name();//手机操作系统
        $data['ip'] = get_client_ip();//ip地址

        $res = $this->guestbookModel->addGuestbook($data);
        if($res){
            
            //发送留言通知
            $this->_cronGuishudi();
            
            return_json(0,"已提交,工作人员会尽快与您联系");
        }
        return_json(1,"提交失败,请重试");
    }
    
    /*
     * 计划任务获取手机号归属地,ip地址信息
     */
    public function _cronGuishudi(){
        
        $where = array(
            "is_cron" => -1,
            "is_delete" => 0,
        );
        $task_list = $this->guestbookModel->getGuestbookList($where,1);
        if (empty($task_list)) {
            return false;
        }
        
        $success_nums = 0;
        $total = count($task_list);

        foreach ($task_list as $k => $msg) {
        
            if(!$msg['guestbook_id']){
                continue;   
            }

            //再次查询数据库判断状态
            $where_msg = array(
                'guestbook_id' => $msg['guestbook_id']
            );
            $dres = $this->guestbookModel->getGuestbook($where_msg); // 取出数据
        
            if(empty($dres)){
                continue;//已被删除
            }
            
            $send_status = $dres["is_cron"];
            if($send_status != -1){
                continue;//已经发送过 或正在发送
            }
            
            //正在发送
            $cron_data = array(
                'is_cron' => 3,//正在发送
            );
            $res = $this->guestbookModel->editGuestbook($where_msg, $cron_data);

            $data = array();
            if($dres['mobile']){
                $mobile_info = guishudi($dres['mobile']);//手机归属地
                if(!empty($mobile_info)){
                    //获取归属地信息成功
                    $data['mobile_com'] = $mobile_info['data']['operator'];//手机运营商
                    if($mobile_info['data']['province'] == $mobile_info['data']['city']){
                        $data['mobile_location'] = $mobile_info['data']['city'];
                    }else{
                        $data['mobile_location'] = $mobile_info['data']['province'].$mobile_info['data']['city'];
                    }
                }
            }

            if($dres['ip']){
                //获取ip地址
                $ip_address = location2($dres['ip'],1);//ip地址
                if($ip_address){
                    $data['ip_location'] = $ip_address;
                }
            }
            
            if (!empty($data)) {
                // 获取成功
                $data['is_cron'] = 1;
                $res = $this->guestbookModel->editGuestbook($where_msg, $data);
                if($res){
                    $success_nums++;
                }
        
            } else {
                // 获取失败
                $cron_data = array(
                    'is_cron' => 2,//
                );
                $res = $this->guestbookModel->editGuestbook($where_msg, $cron_data);
            }
            
            //发送消息cron
            $khdata = $this->guestbookModel->getGuestbook($where_msg); // 再次取出数据
            if(!empty($khdata)){
                
                $GUESTBOOK_UID = C('GUESTBOOK_UID');
                $uids = explode(",",$GUESTBOOK_UID);

                foreach ($uids as $k=>$v){
                    $guid = intval($v);
                    //发给老板
                    if ($guid){
                        $admininfo = $this->getMemberInfobyUid($guid);//
                        if($admininfo){
                            $this->_BookMsg($admininfo,$khdata);
                        }
                    }
                }
            }
            
        }

        $r = array(
            "success_nums" => $success_nums,
            "total" => $total
        );
        
        return $r;

        
    }
    
    
    /*
     * 通知,模板消息
     * 1
     */
    protected function _BookMsg($userinfo,$khdata){
        //发送升级模板消息
        //判断是否关注, 没关注就别发了
        if(!$userinfo['subscribe']){
            //return false;
            //TODO
        }
    
        //发送模板消息
        $msg = $this->_sendBookTmp($userinfo,$khdata);
    
        $msgttt = array(
            "uid" => $userinfo['uid'],
            "openid" => $userinfo['openid'],
            "nickname" => $userinfo['nickname'],
            "groupid" => -1,
            "content" => $msg,
            "send_priority" => 1,
            "create_time" => time(),
            "send_time" => time(),
            "status" => -1,
            "send_plan_time" => time()
        );
        $tmpmsgModel = new tmpmsgModel();
        $tmpmsgModel->addtmpmsg($msgttt);//添加消息到数据库队列
    
        return true;
    }
    
    

    /*
     * 拼接模板消息
     */
    protected function _sendBookTmp($userinfo,$khdata){
    
        $tmp_id = $this->tmpl['KH_MSG']['tmp_id'];
        $color = $this->tmpl['KH_MSG']['color'];
    
        $url = $footer = $header = '';
        

        $nickname = $userinfo['realname'] ? $userinfo['realname'] : $userinfo['nickname'];
        $header = "亲爱的".$nickname.",有新客户留言,请注意查看~".'\\n';
        $header .= "留言内容:".$khdata['content'].'\\n';
        
        $header .= "客户昵称:".$khdata['nickname'];
        
       $footer = "手机品牌:".$khdata['mobile_brand'].'\\n';
       $footer .= "手机系统:".$khdata['mobile_os'].'\\n';
       $footer .= "手机运营商:".$khdata['mobile_com'].'\\n';
       $footer .= "手机归属地:".$khdata['mobile_location'].'\\n';
        
       $footer .= "网络信号:".$khdata['network'].'\\n';
       $footer .= "网络归属地:".$khdata['ip_location'].'\\n';
        
 //       $footer .= "点击查看详情...";
        
        $url = "tel://".$khdata['mobile'];
        
        $mobile = $khdata['mobile'];
        $nickname = $khdata['realname'];
            

        
        $msg = array(
            "touser" => $userinfo['openid'],
            "template_id" => $tmp_id,//
            'url' => $url,
            "topcolor" => $color,
            'data' => array(
                'first' => array(
                    'value' => urlencode($header),
                    'color' => $color
                ),
                'keyword1' => array(
                    'value' => urlencode($nickname),//客户姓名
                    'color' => $color,
                ),
                'keyword2' => array(
                    'value' => urlencode($mobile),//客户手机
                    'color' => $color,
                ),
                'keyword3' => array(
                    'value' => urlencode($khdata['create_time']),//时间
                    'color' => $color,
                ),
                'remark' => array(
                    'value' => urlencode($footer),
                    'color' => $color,
                ),
            ),
        );
    
        return $msg = urldecode(json_encode($msg));
    }
    
    
    
    
    
    
    
}